package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.TestOption;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class TestOptionApiImpl implements TestOptionApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public PageResult findByPage(Integer page, Integer pageSize) {
        Query query = new Query();
        long count = mongoTemplate.count(query, TestOption.class);
        query.skip((page - 1) * pageSize).limit(pageSize);
        List<TestOption> list = mongoTemplate.find(query, TestOption.class);
        return new PageResult(page, pageSize, count, list);
    }

    @Override
    public Boolean deleteById(String id) {
        try {
            Criteria criteria = Criteria.where("id").is(new ObjectId(id));
            Query query = Query.query(criteria);
            mongoTemplate.remove(query, TestOption.class);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public TestOption findById(String id) {
        TestOption option = mongoTemplate.findById(new ObjectId(id), TestOption.class);
        return option;
    }

    @Override
    public void save(TestOption options) {
        mongoTemplate.save(options);
    }

    @Override
    public void update(TestOption options, String optionId) {
        Criteria criteria = Criteria.where("id").is(new ObjectId(optionId));
        Query query = Query.query(criteria);
        Update update = new Update();
        update.set("questionId", options.getQuestionId());
        update.set("option", options.getOption());
        update.set("nature", options.getNature());
        mongoTemplate.updateFirst(query, update, TestOption.class);
    }

    @Override
    public void deleteByQuestionId(String questionId) {
        Criteria criteria = Criteria.where("questionId").is(questionId);
        Query query = Query.query(criteria);
        mongoTemplate.remove(query, TestOption.class);
    }

    @Override
    public Map<String, List<String>> findByQuestionIds(List<String> queIds) {
        Map<String, List<String>> map = new HashMap<>();
        for (String queId : queIds) {
            Criteria criteria = Criteria.where("questionId").is(queId);
            Query query = Query.query(criteria);
            List<TestOption> list = mongoTemplate.find(query, TestOption.class);
            //获取问题的选项内容
            List<String> options = CollUtil.getFieldValues(list, "option", String.class);
            map.put(queId, options);

        }
        return map;
    }
}